package com.yin.project.admin.rbac.controller;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.yin.project.admin.rbac.model.RoleEntity;
import com.yin.project.admin.rbac.service.RoleService;

/**
 * Created by Eason on 2016/12/1.
 */
@Controller
@RequestMapping(value = "/role")
public class RoleController {

    @Autowired
    private RoleService	 roleService;
    /**
     * 查询所有角色
     */
    @RequestMapping(value = "/_", method = RequestMethod.GET)
    @ResponseBody
    public List<RoleEntity> getAll() {
       return null;
    }
    /**
     * 保存角色
     */
    @RequestMapping(value = "/_", method = RequestMethod.POST)
    @ResponseBody
    public String addRole(@ModelAttribute("role") RoleEntity role){
    	if(StringUtils.isNotEmpty(role.getRoleName())){
			roleService.addRole(role);
			return "ok";
    	}else{
    		return "error";
    	}
    }
    /**
     * 修改角色页面
     * @return
     */
    @RequestMapping(value = "/_/{id}", method = RequestMethod.GET)
    @ResponseBody
    public RoleEntity updateRoleView(@PathVariable("id") String id) {
    	RoleEntity	roleEntity  = roleService.queryRoleById(id);
        return roleEntity;
    }
    /**
     * 修改角色
     */
    @RequestMapping(value = "/_/{id}", method = RequestMethod.PUT)
    @ResponseBody
    public String updateRole(@PathVariable("id") String id,@RequestBody String body){
    	RoleEntity roleEntity =JSON.parseObject(body, RoleEntity.class);
    	roleEntity.setId(id);
    	roleService.updateRole(roleEntity);
        return "ok";
    }
    /**
     * 删除角色
     */
    @RequestMapping(value = "/_/{id}", method = RequestMethod.DELETE)
    public String deleteRole(@PathVariable("id") String id){
    	roleService.deleteRole(id);
    	return "ok";
    }
}
